package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.github.pagehelper.PageInfo;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.JsonUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.account.ILocalAccountService;
import com.iwhalecloud.bss.kite.cucc.client.dto.account.CustAccountDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.account.QryCustAccountReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.DictionaryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDTO;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.DictionaryCache;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.dataservice.util.IdUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstAcct;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <Description> 过户信息-账户名称处理 <br>
 *
 * @author wei.yanhong<br>
 * @version 1.0<br>
 * @since R9.0<br>
 */
@Service
public class TransfAccountDealHandler extends AbstractAttrHandler {

    @Autowired
    private ILocalAccountService accountService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }

    /**
     * 修改账户属性
     */
    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        //value有值,即联动账户相关属性,并联动付费关系的账户
        if (KiteStringUtils.isNotEmpty(value)) {
            Map<String, Object> extParameter = querySceneInstService.getExtParameter(sceneInstId);
            Object newCust = extParameter.get(sceneInstId + "_transNewCustDTO");
            CuccCustInfoDTO newCustDto = Objects.nonNull(newCust) ? (CuccCustInfoDTO) newCust : new CuccCustInfoDTO();
            if (!KiteObjectUtils.isEmpty(newCustDto)) {
                PageInfo<QryCustAccountReq> list = accountService
                    .listCustAccountInfo(newCustDto.getCustId(), "", "", "", "", "", "", newCustDto.getEparchyProvince(), newCustDto.getEparchyCity(),null, 1, 10);
                if (!KiteObjectUtils.isEmpty(list) && KiteListUtils.isNotEmpty(list.getList())) {
                    List<QryCustAccountReq> custAccountDTOList = list.getList();
                    CustAccountDTO custAccount = new CustAccountDTO();
                    if (KiteListUtils.isNotEmpty(custAccountDTOList)) {
                        for (int i = 0; i < custAccountDTOList.size(); i++) {
                            custAccount = JsonUtils
                                .parseObject(KiteMapUtils.getString((Map) custAccountDTOList.get(i), "custAccount"),
                                    CustAccountDTO.class);
                            if (custAccount.getAcctId().equals(value)) {
                                break;
                            }
                        }
                    }
                    if (!KiteObjectUtils.isEmpty(custAccount)) {
                        // 2 联动设置其他客户属性
                        operateSceneCommonService.setCommonAttrValue(sceneInstId, "accountId", KiteStringUtils.valueOf(custAccount.getAcctId()),
                            KiteStringUtils.valueOf(custAccount.getAcctId()));
                        operateSceneCommonService
                            .setCommonAttrValue(sceneInstId, "acctContractCode", custAccount.getCountyCode(),
                                custAccount.getCountyCode());
                        operateSceneCommonService.setCommonAttrValue(sceneInstId, "cbssAcctId", KiteStringUtils.valueOf(custAccount.getCustId()),
                            KiteStringUtils.valueOf(custAccount.getCustId()));
//                        long lt = new Long(custAccount.getOpenDate());
//                        Date date = new Date(lt);
                        SimpleDateFormat df = new SimpleDateFormat(DateFormatUtils.DATE_FORMAT);
//                        operateSceneCommonService
//                            .setCommonAttrValue(sceneInstId, "openDate", custAccount.getOpenDate(), df.format(date));
                        operateSceneCommonService
                            .setCommonAttrValue(sceneInstId, "provinceAcctId", custAccount.getProvinceAcctId(),
                                custAccount.getProvinceAcctId());
                        operateSceneCommonService
                            .setCommonAttrValue(sceneInstId, "provinceCode", custAccount.getProvinceCode(),
                                LocalAttrHelper.getRegionName(custAccount.getProvinceCode()));
                        operateSceneCommonService
                            .setCommonAttrValue(sceneInstId, "eparchyCode", custAccount.getEparchyCode(),
                                LocalAttrHelper.getRegionName(custAccount.getEparchyCode()));
                        List<DictionaryDTO> dictionaryDTOList = DictionaryCache.get("BUS10007");
                        String payModeDesc = "";
                        if (KiteListUtils.isNotEmpty(dictionaryDTOList)) {
                            String payModeCode = custAccount.getPayModeCode();
                            payModeDesc = dictionaryDTOList.stream()
                                .filter(dictionaryDTO -> KiteStringUtils.equals(payModeCode, dictionaryDTO.getCode()))
                                .findFirst().map(DictionaryDTO::getValue).orElse("");
                            operateSceneCommonService
                                .setCommonAttrValue(sceneInstId, "payMode", custAccount.getPayModeCode(), payModeDesc);
                        }

                        // 联动付费关系中的账户信息,生成新的账户实例关系
                        List<KiteOfferInstAcct> allAcctRels = new ArrayList<KiteOfferInstAcct>();
                        KiteOfferInstAcct offerInstAcctRel = new KiteOfferInstAcct();
                        offerInstAcctRel.setInstId(IdUtils.generateInstId(InstType.PROD_INST_ACCT));
                        offerInstAcctRel.setAcctId(KiteStringUtils.valueOf(custAccount.getAcctId()));
                        offerInstAcctRel.setAcctName(custAccount.getPayName());
                        offerInstAcctRel.setAcctType(custAccount.getPayModeCode());
                        offerInstAcctRel.setAcctItemId(custAccount.getPayModeCode());
                        offerInstAcctRel.setIsDefault(custAccount.getIsGroupAcct());
                        offerInstAcctRel.setAccountCycle(custAccount.getAccountCycle());
                        offerInstAcctRel.setFeeCycle(custAccount.getPayCycle());
                        offerInstAcctRel.setPayWay("");
                        offerInstAcctRel.setPayLimit("");
//                        offerInstAcctRel.setStartDate(custAccount.getCreateDate());
//                        offerInstAcctRel.setEndDate(custAccount.getCreateDate());
                        offerInstAcctRel.setInstType(InstType.OFFER_INST_ACCT);
                        offerInstAcctRel.setActionType(ActionType.A);
                        allAcctRels.add(offerInstAcctRel);
                        operateSceneInstService.putExtParameter(sceneInstId, "offerInstAcctRels", allAcctRels);
                    }
                }
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

}
